﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithm.Struct
{
    public class Stack<T>
    {
        private T[] _stack;
        private int _top = -1;
        private int _lenght = 0;
        public Stack(int length)
        {
            if (length <= 0)
            {
                throw new ArgumentException();
            }
            _lenght = length;
            _stack = new T[length];
        }


        public bool IsEmpty()
        {
            return _top < 0;
        }

        public void Push(T t)
        {
            if (_top + 1 >= _lenght)
            {
                throw new StackOverflowException();
            }
            _top++;
            _stack[_top] = t;
        }

        public T Top()
        {
            if (IsEmpty())
            {
                throw new Exception("underflow");
            }
            return _stack[_top];
        }

        public T Pop()
        {
            if (IsEmpty())
            {
                throw new Exception("underflow");
            }
            _top--;
            return _stack[_top+1];
        }
    }
}
